﻿ <?php
/**********************************************************
** Lime Manager
** Thomas Knobloch & Clement Troesch
** Manager Ajax Users View
***********************************************************/
?>
<?php include_once "../../model/DevelopperModel.php"; ?>
<?php include_once "../../model/SessionModel.php"; ?>
<?php include_once "../../model/DevelopperModel.php"; ?>
<?php include_once "../main/QueryTableView.php"; ?>
<?php session_start(); ?>

<div class="section_title" id="objects"><h1> Requêtes </h1></div>
<div class="section_info">
	Saisir une requête puis en afficher le plan d'exécution et les sauvegarder.
</div>

<div class="article">

<div class="section_title_small_blue"><h4>Saisir une requête</h4></div>

<textarea class="queryArea" id="query" ><?php echo isset($_GET['query'])?($_GET['query']):"" ?></textarea>
<br />
<input type="button" value="Envoyer" onClick="displayQueries($('#query').val())" />
<br /><br />



<?php



if(isset($_GET['query']))
{
	$query = $_GET['query'];
	
	?>
	<div class="section_title_small_green"><h4>Résultats</h4></div>
	
	<div class="section_title_small_blue"><h4>Tables</h4></div>
	
	<div class="querryError">
		<?php
		$nbRow = $_SESSION['session']->currentConnection->computeDataArrayWithColumnNames($query, $result, $columns)
		?>
	</div>
	
	<?php
	if($nbRow != null)
	{
		displayResult($result, $columns);
		?>
		<script type="text/javascript">
			//JQuery main entry point
			$(document).ready(function() {
				$(".querryError").hide();
			});
		</script>
		
		<div class="section_title_small_blue"><h4>Plan d'exécution</h4></div>
		
		<div class="sql_code">
		<pre class="sh_sql">
		
		<?php
		echo "\n";
		$nbRow = DevelopperModel::getExplainPlan($_SESSION['session']->currentConnection, $plan, $query);
		$lines = "";
		foreach ($plan as $row) {
			if($_SESSION['session']->currentConnection->type == "ORACLE")
				$lines = $lines.$row['Query Plan']."\n";
			else
			{
				$lines = $lines.$row['id']." "
				.$row['select_type']." "
				.$row['table']." "
				.(isset($row['type'])?"type=".$row['type']." ":"")
				.(isset($row['possible_keys'])?"possible_keys=".$row['possible_keys']." ":"")
				.(isset($row['key'])?"key=".$row['key']." ":"")
				.(isset($row['key_len'])?"key_len=".$row['key_len']." ":"")
				.(isset($row['ref'])?"ref=".$row['ref']." ":"")
				.(isset($row['rows'])?"rows=".$row['rows']." ":"")
				.$row['Extra']." "
				."\n";
			}
		}
		echo htmlentities($lines);
		?>
		
		</pre>
		</div>
		
		<a href="view/main/DownloadView.php?name=Save.sql&text=<?php echo urlencode($lines) ?>" target="_blank"><input type="button" value="Télécharger" /></a>
		
		<script type="text/javascript" src="js/sql/shjs.js"></script>
		<script type="text/javascript" src="js/sql/sh_sql.js"></script>
		<script type="text/javascript">

			//JQuery main entry point
			 $(document).ready(function() {
				//Highlight query
				sh_highlightDocument();
			 });
		</script>
		<?php
	}
}
?>


